﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreRay.RenderRegionManagers
{
    public class BasicRenderRegionManager : RenderRegionManager
    {
        private int currentPositionX;
        private int currentPositionY;

        public BasicRenderRegionManager(int totalWidth, int totalHeight) : this(64, 64, totalWidth, totalHeight)
        {

        }

        public BasicRenderRegionManager(int regionWidth, int regionHeight, int totalWidth, int totalHeight) : base(regionWidth, regionHeight, totalWidth, totalHeight)
        {

        }

        public override RenderRegion GetNextRenderRegion()
        {
            int regionWidth = (this.TotalWidth - this.currentPositionX) >= this.RegionWidth ? this.RegionWidth : this.TotalWidth - this.currentPositionX;
            int regionHeight = (this.TotalHeight - this.currentPositionY) >= this.RegionHeight ? this.RegionHeight : this.TotalHeight - this.currentPositionY;

            RenderRegion renderRegion = null;

            if (this.currentPositionY < this.TotalHeight)
            {
                renderRegion = new RenderRegion(this.currentPositionX, this.currentPositionY, regionWidth, regionHeight);
            }

            this.currentPositionX += this.RegionWidth;

            if(this.currentPositionX >= this.TotalWidth)
            {
                this.currentPositionX = 0;
                this.currentPositionY += this.RegionHeight;
            }

            return renderRegion;
        }
    }
}
